package com.xian.im.modules.im.core.impl;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.xian.im.common.model.rule.IRuleServerNode;
import com.xian.im.modules.distributed.ContextInstance;
import com.xian.im.modules.im.core.IRuleImService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @program: xian-code
 * @description:  空实现。第二版实现
 * @author: liru.xian
 * @create: 2021-01-18 19:57
 **/
@Slf4j
@Service
public class IRuleImServiceImpl implements IRuleImService {

    // 读写锁
    private static  final ReentrantReadWriteLock RTLOCK = new ReentrantReadWriteLock();

    private final ContextInstance contextInstance;

    public IRuleImServiceImpl(ContextInstance contextInstance) {
        this.contextInstance = contextInstance;
    }

    /**
     * 获取server
     *
     * @param mark
     * @return
     */
    @Override
    public IRuleServerNode getIruleServer(String mark) {
        RTLOCK.readLock().lock();
        try {
            Instance server = contextInstance.getServer(mark);
            IRuleServerNode node = new IRuleServerNode();
            node.setPort(server.getPort()+"");
            node.setHost(server.getIp());
        }catch (Exception e){
            log.error("获取 IRuleServerNode 异常{}",e.getMessage());
        }finally {
            RTLOCK.readLock().unlock();
        }
        return null;
    }

    /**
     * 获取集群server
     *
     * @return
     */
    @Override
    public List<IRuleServerNode> getServers() {
        RTLOCK.readLock().lock();
        List<IRuleServerNode> list = new ArrayList<>();
        try {
            List<Instance> instances = contextInstance.instances();
            if(!instances.isEmpty()){
                for (Instance instance : instances) {
                    IRuleServerNode node = new IRuleServerNode();
                    node.setHost(instance.getIp());
                    node.setPort(instance.getPort()+"");
                    list.add(node);
                }
            }
            return list;
        }catch (Exception e){
            log.error("获取 IRuleServerNode 异常{}",e.getMessage());
        }finally {
            RTLOCK.readLock().unlock();
        }
        return null;
    }

    /**
     * 注册
     * @param server
     * @return
     */
    @Override
    public Boolean register(IRuleServerNode server) {
        RTLOCK.writeLock().lock();
        try {

            return Boolean.TRUE;
        }catch (Exception e){
            log.error("register  IRuleServerNode {}",e.getMessage());
        }finally {
            RTLOCK.writeLock().unlock();
        }
        return Boolean.FALSE;
    }

    /**
     * 剔除
     *
     * @param server
     * @return
     */
    @Override
    public Boolean weedOut(IRuleServerNode server) {
        RTLOCK.writeLock().lock();
        try {

            return Boolean.TRUE;
        }catch (Exception e){
            log.error("register  IRuleServerNode {}",e.getMessage());
        }finally {
            RTLOCK.writeLock().unlock();
        }
        return Boolean.FALSE;
    }
}
